﻿    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace B13_Ex03_David_300512340_Alexey_321203689
{
    public partial class AlbumForm : Form
    {
        private AlbumFormLogic m_AlbumFormLogic;
        private AlbumWrapper m_SelectedAlbum;
        private List<Image> m_ImagesList; // The slide show take the images from this list.
        private int m_Index = 0; // Index for the listBoxPictures, used when click on buttonSlideShow. 

        public AlbumForm()
        {
            InitializeComponent();
        }

        public AlbumForm(AlbumWrapper i_SelectedAlbum)
        {
            InitializeComponent();
            m_SelectedAlbum = i_SelectedAlbum;
            init();
        }

        private void init()
        {
            m_AlbumFormLogic = new AlbumFormLogic();
            m_ImagesList = new List<Image>();
            m_AlbumFormLogic.AttachObserver(new ReportFinishedLoadingOnePhotoDelegate(this.increaseProgressBar));
            m_AlbumFormLogic.AttachObserver(new ReportFinishedLoadingPhotosDelegate(this.startSlideShow));
            m_AlbumFormLogic.AttachObserver(new ReportFinishedLoadingPhotosDelegate(this.enableSlideShowButtons));
            m_AlbumFormLogic.AttachObserver(new ReportPhotoLoadingCanceledDelegate(this.changeControlsProperties));
            listBoxPictures.DataSource = m_SelectedAlbum.GetAlbumPhotosList();
            if (listBoxPictures.SelectedItems.Count == 1)
            {
                pictureBoxFromAlbum.LoadAsync((listBoxPictures.Items[0] as PhotoWrapper).GetPhotoURL);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            if (m_AlbumFormLogic.m_BackgroundWorkerLoader.IsBusy)
            {
                threadStoper();
            }
        }

        private void buttonSaveSelectedPhoto_Click(object sender, EventArgs e)
        {
            m_AlbumFormLogic.SaveSelectedPhotos(getPath(), textBoxPictueSaveName.Text, listBoxPictures);
        }

        private void buttonSaveAlbum_Click(object sender, EventArgs e)
        {
            m_AlbumFormLogic.SaveAlbum(m_SelectedAlbum, getPath(), textBoxPictueSaveName.Text);
        }

        private string getPath()
        {
            folderBrowserDialog.ShowDialog();

            return folderBrowserDialog.SelectedPath;
        }

        private void listBoxPictures_MouseUp(object sender, MouseEventArgs e)
        {
            int point = listBoxPictures.IndexFromPoint(e.X, e.Y);
            PhotoWrapper item = point >= 0 ? listBoxPictures.Items[point] as PhotoWrapper : null; // If the album is empty, null.
            PhotoWrapper photo = null;

            if (listBoxPictures.SelectedItems.Contains(item))
            {
                if (m_ImagesList.Count > 0)
                {
                    pictureBoxFromAlbum.Image = m_ImagesList[listBoxPictures.SelectedIndex];
                    m_Index = listBoxPictures.SelectedIndex;
                }
                else
                {
                    if (listBoxPictures.SelectedItems.Count == 1)
                    {
                        photo = item;
                        pictureBoxFromAlbum.LoadAsync(photo.GetPhotoURL);
                    }
                }
            }
        }

        private void buttonSlideShow_Click(object sender, EventArgs e)
        {
            if (m_ImagesList.Count == 0)
            {
                changeControlsProperties();
                m_AlbumFormLogic.m_BackgroundWorkerLoader.RunWorkerAsync(m_SelectedAlbum.GetAlbumPhotosList());
            }
            else
            {
                startSlideShow();
            }
        }

        private void changeControlsProperties()
        {
            if (buttonResetSlideShow.Text == "Reset slide show")
            {
                buttonResetSlideShow.Text = string.Format("Cancel");
            }
            else
            {
                buttonResetSlideShow.Text = string.Format("Reset slide show");
            }

            toggleControls();
        }

        private void toggleControls()
        {
            listBoxPictures.Enabled = !listBoxPictures.Enabled;
            textBoxPictueSaveName.Enabled = !textBoxPictueSaveName.Enabled;
            buttonSaveSelectedPhoto.Enabled = !buttonSaveSelectedPhoto.Enabled;
            buttonSaveAlbum.Enabled = !buttonSaveAlbum.Enabled;
            comboBoxSlideTime.Enabled = !comboBoxSlideTime.Enabled;
            buttonSlideShow.Enabled = !buttonSlideShow.Enabled;
            buttonResetSlideShow.Enabled = !buttonResetSlideShow.Enabled;
        }

        private void startSlideShow()
        {
            int timeSlide;

            if (listBoxPictures.Items.Count > 1)
            {
                try
                {
                    timeSlide = int.Parse(comboBoxSlideTime.Text) * 1000;
                }
                catch
                {
                    MessageBox.Show("Invalid input. Default interval 2 seconds will be used.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    timeSlide = 2000;
                }

                m_ImagesList = m_AlbumFormLogic.ImagesList;
                timerSlideShow.Interval = timeSlide;
                timerSlideShow.Enabled = !timerSlideShow.Enabled;
                toggleButtonPlaySlideShow();
                if (buttonSlideShow.Text == "Play slide show")
                {
                    listBoxPictures.Enabled = true;
                    textBoxPictueSaveName.Enabled = true;
                    buttonSaveSelectedPhoto.Enabled = true;
                    buttonSaveAlbum.Enabled = true;
                    comboBoxSlideTime.Enabled = true;
                }

                if (buttonSlideShow.Text == "Stop slide show")
                {
                    listBoxPictures.Enabled = false;
                    textBoxPictueSaveName.Enabled = false;
                    buttonSaveSelectedPhoto.Enabled = false;
                    buttonSaveAlbum.Enabled = false;
                    comboBoxSlideTime.Enabled = false;
                }

                buttonResetSlideShow.Enabled = !buttonResetSlideShow.Enabled;
            }
            else
            {
                MessageBox.Show("Only one picture in this album.", "Cant play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
        }

        private void toggleButtonPlaySlideShow()
        {
            buttonSlideShow.Text = buttonSlideShow.Text == "Play slide show" ? "Stop slide show" : "Play slide show";
        }

        private void timerTick(object sender, EventArgs e)
        {
            if (m_Index < listBoxPictures.Items.Count)
            {
                pictureBoxFromAlbum.Image = m_ImagesList[m_Index];
                listBoxPictures.ClearSelected();
                listBoxPictures.SelectedIndex = m_Index;
                m_Index++;
            }
            else
            {
                m_Index = 0;
                timerSlideShow.Stop();
                toggleControls();
                toggleButtonPlaySlideShow();
                buttonSlideShow.Enabled = true;
            }
        }

        private void buttonResetSlideShow_Click(object sender, EventArgs e)
        {
            if (m_AlbumFormLogic.m_BackgroundWorkerLoader.IsBusy)
            {
                threadStoper();
            }
            else
            {
                timerSlideShow.Stop();
                m_Index = 0;
                listBoxPictures.ClearSelected();
                listBoxPictures.SelectedIndex = m_Index;
                pictureBoxFromAlbum.Image = m_ImagesList[m_Index];
            }
        }

        private void enableSlideShowButtons()
        {
            buttonResetSlideShow.Text = string.Format("Reset slide show");
            buttonSlideShow.Enabled = !buttonSlideShow.Enabled;
        }

        private void increaseProgressBar()
        {
            progressBarImageLoader.Value = m_AlbumFormLogic.ProgressBarValue;
        }

        private void threadStoper()
        {
            m_AlbumFormLogic.m_BackgroundWorkerLoader.CancelAsync();
        }
    }
}